//
//  TXFComposeToolBar.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/3.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

enum ComposeToolBarType: Int{

    case Picture =  10

    case Mention = 20
    
    case Trend = 30
    
    case Emoticon = 40
  
    case Add = 50

}



class TXFComposeToolBar: UIView {
    var emoticonBtn : UIButton?
    
    //设置表情键盘按钮的不同的显示图片
    
    var isEmoticon : Bool = false{
    
        didSet{
        //显示键盘(系统)
            if isEmoticon{
            emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
            emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            
            }else{
            
                emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            
            
            
            }
        
        
        
        
        }
    
    
    
    }
    
    
    
    //定义闭包传递按钮
    var  clouseback: ((type: ComposeToolBarType)->())?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

 required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
 }
    func setupUI(){
    
    
        //设置按钮
        
     let pictureBtn = addChirdButton("compose_toolbar_picture", type: .Picture)
        
     let mentionBtn = addChirdButton("compose_mentionbutton_background", type: .Mention)
        
     let trendBtn = addChirdButton("compose_trendbutton_background", type: .Trend)
        //设置按钮的选中图片
   emoticonBtn = addChirdButton("compose_emoticonbutton_background", type: .Emoticon)
        
     let addBtn = addChirdButton("compose_add_background", type: .Add)
        
        //设置约束
        
        pictureBtn.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        }
        mentionBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBtn.snp_trailing)
            make.width.equalTo(trendBtn)
        }
        trendBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionBtn.snp_trailing)
            make.width.equalTo(emoticonBtn!)
        }
        emoticonBtn!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendBtn.snp_trailing)
            make.width.equalTo(addBtn)
        }
        
        addBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonBtn!.snp_trailing)
        }
    }
    
    
    
    //通过设置方法来添加按钮:
    
    func addChirdButton(imageName: String,type : ComposeToolBarType) ->UIButton{
    
    let  button = UIButton()
    
    button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
    
        
    button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
    button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
    button.tag  = type.rawValue
    
    button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    addSubview(button)
        
    return button
    }
    
    
    func buttonClick(button: UIButton){
    
    //我们要在控制器中实现按钮的点击事件:
   //调用闭包
        clouseback?(type: ComposeToolBarType(rawValue: button.tag)!)
    
    }
    
    
    

}
